package com.ws.app.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ws.app.entity.User;
import com.ws.app.service.UserService;
import com.ws.base.common.util.WSUtil;
import com.ws.base.db.common.order.Order;
import com.ws.base.db.common.page.Page;
import com.ws.base.db.support.DBSqlIdSupport;

public class UserServiceImpl implements UserService {
    private DBSqlIdSupport dbSqlIdSupport;

    @Override
    public boolean addUser(User user) {
        dbSqlIdSupport.add("app_addUser", user.toMap());
        return false;
    }

    @Override
    public boolean deleteUser(String id) {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("id", id);
        dbSqlIdSupport.delete("app_deleteUserkById", condition);
        return false;
    }

    @Override
    public boolean updateUser(User user) {
        dbSqlIdSupport.update("app_updateUserById",
                WSUtil.removeEmpty(user.toMap()));
        return false;
    }

    @Override
    public List<User> queryAllExpense(Map<String, Object> condition, Page page,
            Order order) {
        return this.mToUser(dbSqlIdSupport.query("app_queryUserByCondition",
                condition, page, order));
    }

    @Override
    public User getUser(Map<String, Object> condition) {
        List<User> users = queryAllExpense(condition, null, null);
        if (users.isEmpty()) {
            return null;
        } else {
            return users.get(0);
        }
    }

    private List<User> mToUser(List<Map<String, Object>> list) {
        List<User> users = new ArrayList<User>();
        User user = null;
        for (Map<String, Object> map : list) {
            user = new User();
            user.setId(WSUtil.toStr(map.get("id")));
            user.setName(WSUtil.toStr(map.get("name")));
            user.setPassword(WSUtil.toStr(map.get("password")));
            user.setRemark(WSUtil.toStr(map.get("remark")));
            users.add(user);
        }

        return users;
    }

    public DBSqlIdSupport getDbSqlIdSupport() {
        return dbSqlIdSupport;
    }

    public void setDbSqlIdSupport(DBSqlIdSupport dbSqlIdSupport) {
        this.dbSqlIdSupport = dbSqlIdSupport;
    }

}
